﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Drawing;
using System.Collections.ObjectModel;
using System.Linq.Expressions;


public partial class Details : System.Web.UI.Page
{
    private Game spel = new Game();
    int klantid = 0;
    int id = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            id = int.Parse(Request.QueryString["id"]);
        }
        
        TextBox1.TextMode = TextBoxMode.MultiLine;
        TextBox2.TextMode = TextBoxMode.MultiLine;

        cover.Attributes.Add("onclick", "cover_click()");
        cover_close.Attributes.Add("onclick", "closePopup()");
        review_close.Attributes.Add("onclick", "review_closePopup()");

        DataClassesDataContext db = new DataClassesDataContext();

        if (!Request.IsAuthenticated)
        {           
            review.Attributes.Add("onclick", "niet_ingelogd()");
            login_close.Attributes.Add("onclick", "login_closePopup()");    
        }
        else
        {
            
            review.Attributes.Add("onclick", "review_click()");
            MembershipUser currentUser;
            currentUser = Membership.GetUser();
            klantid = int.Parse(currentUser.ProviderUserKey.ToString());

            var beoordelingen =
                (from b in db.Beoordelings
                where b.GAME_NR == id && b.KLANT_ID == klantid
                select b).Count();

            if (beoordelingen < 1)
            {
                review.Attributes.Add("onclick", "review_click()");
            }
            else
            {
                review.Text = "U heeft reeds een review geplaatst.";
                wijzig.Text = "Wijzig review";
                wijzig.Attributes.Add("onclick", "wijzigen_click()");
                wijzig_close.Attributes.Add("onclick", "wijzig_closePopup()");    
                review.ForeColor = Color.Black;
                review.Attributes.Remove("OnClick");
                if (!IsPostBack)
                {
                    var review1 =
                    from r in db.Reviews
                    where r.GAME_NR == id
                    join k in db.Klants on r.KLANT_ID equals k.KLANT_ID
                    where k.KLANT_ID == klantid
                    select new { r.TEKST };

                    var test = review1.First();

                    TextBox2.Text = test.TEKST;
                }
            }
        }

        var games2 =
        from g in db.Games
        where g.GAME_NR == id
        select g;

        foreach (Game spelletje in games2)
        {
            beschrijving_text.Text = spelletje.BESCHRIJVING;
            var test = spelletje.Maker;
            var makernaam = test.NAAM;
            
            details_game_text.Text = title_text.Text = spelletje.TITEL;
            title_text.Text = "<h2>" + spelletje.TITEL + "</h2>";
            cover.ImageUrl = spelletje.IMAGE;
            cover_plaatje.ImageUrl = spelletje.IMAGE;
            platform_text.Text = spelletje.Platform.OMSCHRIJVING;
            beschrijving_text.Text = spelletje.BESCHRIJVING;
            prijs_text.Text = spelletje.PRIJS.ToString();
            bestandsgrootte_game_text.Text = spelletje.BESTANDSGROOTTEGAME.ToString() + "Mb";
            bestandsgroote_cover_text.Text = spelletje.BESTANDSGROOTTEAFBEELDING.ToString() + "Mb";
            spel = spelletje;
        }

        var reviews =
            from r in db.Reviews
            where r.GAME_NR == id
            join k in db.Klants on r.KLANT_ID equals k.KLANT_ID
            orderby r.DATUM descending
            select new { r.TEKST,r.DATUM,k.EMAILADRES };

        GridView1.DataSource = reviews;
        GridView1.DataBind();


        var reviewscore =
                             from b in db.Beoordelings
                             where b.GAME_NR == id
                             select b;
        double count = 0;
        var teller = 0;
        foreach (Beoordeling cijfer in reviewscore)
        {
            count += cijfer.CIJFER;
            teller++;
        }
        hiddenscore.Value = (count / teller).ToString();
        string labeltjescore = Math.Round((count / teller), 2).ToString();
        if (labeltjescore != "NaN")
        {
            aantalstemmen.Text = teller.ToString() + " Stemmen | Gemiddelde " + labeltjescore;
        }
        else{
            aantalstemmen.Text = "0 Stemmen | Gemiddelde 0";
        }

        var winkelwagengame =
                     from w in db.Winkelwagengames
                     where w.GAME_NR == id
                     select w;

        foreach (Winkelwagengame game_nr in winkelwagengame)
        {
            var anderegames =
                     (from w2 in db.Winkelwagengames
                     where w2.WINKELWAGEN_ID == game_nr.WINKELWAGEN_ID && w2.GAME_NR != id
                     select w2).Take(3).ToList();

            List<Game> naam = new List<Game>();
            List<Platform> platform = new List<Platform>();
            foreach (Winkelwagengame games in anderegames)
            {
                naam.Add(games.Game);
            }
            GridView2.DataSource = naam;
            GridView2.DataBind();
        }
    }

    protected void cover_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MultiView1.ActiveViewIndex = int.Parse(e.Item.Value);
        
    }

    protected void add_review_Click1(object sender, EventArgs e)
    {
        int score = int.Parse(HiddenField1.Value);
        string review = TextBox1.Text.ToString();
        DataClassesDataContext db = new DataClassesDataContext();

        Review reviewobj = new Review();
        reviewobj.KLANT_ID = klantid;
        reviewobj.TEKST = review;
        reviewobj.GAME_NR = id;
        reviewobj.DATUM = DateTime.Now;
        db.Reviews.InsertOnSubmit(reviewobj);

        Beoordeling beoordelingobj = new Beoordeling();
        beoordelingobj.KLANT_ID = klantid;
        beoordelingobj.GAME_NR = id;
        beoordelingobj.CIJFER = score;
        beoordelingobj.DATUM = DateTime.Now;
        db.Beoordelings.InsertOnSubmit(beoordelingobj);

        db.SubmitChanges();

        Response.Redirect("details.aspx?id=" + id);

    }

    protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void koop_Click(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            List<Game> games = (List<Game>)Session["winkelwagen"];
            Boolean magKopen = false;
            if (games == null)
            {
                games = new List<Game>();
                games.Add(spel);
                magKopen = true;
            }
            else
            {
                magKopen = true;
                foreach (Game gamepje in games)
                {
                    if (gamepje.GAME_NR == spel.GAME_NR)
                    {
                        magKopen = false;
                    }
                }
                if (magKopen)
                {
                    games.Add(spel);
                }
            }

            if (magKopen)
            {
                Session.Add("winkelwagen", games);
                koop_niet.Text = "Uw aankoop is in de winkelwagen geplaatst.";
                this.Master.refreshControl();
            }
            else
                koop_niet.Text = "Deze game staat reeds in de winkelwagen.";

        }
        else
        {
            koop_niet.Text = "U dient eerst in te loggen.";
        }
    }
    protected void wijzigKnop_Click(object sender, EventArgs e)
    {
        string review = TextBox2.Text;
        DataClassesDataContext db = new DataClassesDataContext();
        var reviewTekst = from r in db.Reviews
                         where r.GAME_NR == id
                         join k in db.Klants on r.KLANT_ID equals k.KLANT_ID
                         where k.KLANT_ID == klantid
                         select r;

        foreach (Review reviews in reviewTekst)
        {
            reviews.TEKST = review;
        }
        db.SubmitChanges();
        Response.Redirect("details.aspx?id=" + id);

    }
    protected void cover_koop_knop_Click(object sender, ImageClickEventArgs e)
    {
        Boolean magKopen = false;
        if (Request.IsAuthenticated)
        {
            List<Game> covers = (List<Game>)Session["winkelwagenCover"];
            if (covers == null)
            {
                covers = new List<Game>();
                covers.Add(spel);
                magKopen = true;
            }
            else
            {
                magKopen = true;
                foreach (Game gamepje in covers)
                {
                    if (gamepje.GAME_NR == spel.GAME_NR)
                    {
                        magKopen = false;
                    }
                }
                if(magKopen)
                    covers.Add(spel);
            }
            if (magKopen)
            {
                Session.Add("winkelwagenCover", covers);
                koop_niet.Text = "Uw aankoop is in de winkelwagen geplaatst.";
                this.Master.refreshControl();
            }
            else
                koop_niet.Text = "Dit hoesje staat reeds in de winkelwagen.";
        }
        else
        {
            koop_niet.Text = "U dient eerst in te loggen.";
        }
    }
}

